<?php 
	header('content-type:text/html;charset=utf-8');
	//这是一个信息增 删和改操作的处理页

	//1.导入配置文件
	require('config.php');

	//2.链接数据库,并选择选择数据库
	$link = @mysql_connect(HOST,USER,PWD) or die('连接数据库失败!');

	mysql_select_db(DBNAME);

	mysql_set_charset(UTF);

	//3.根据需要action的值,来判断所属操作,执行对应的代码
	switch($_GET['action']){

		case 'add': //执行添加
			//1.获取要添加的信息
			$title = $_POST['title'];
			$keywords = $_POST['keywords'];
			$author = $_POST['author'];
			$time = time();
			$content = $_POST['content'];
			//2.做信息过滤
			//3.准备sql语句,并执行该操作
			$sql = "insert into news(title,keywords,author,addtime,content) values ('$title','$keywords','$author','$time','$content')";


			$result = mysql_query($sql);
			//4.判断是否成功
			if(mysql_affected_rows()>0){

				echo "<script type='text/javascript'>alert('添加成功');window.location='./index.php'</script>";
			}else{

				echo "<script type='text/javascript'>alert('添加失败');window.location='./add.php'</script>";
			}


		break;

		case 'del': //执行删除
			$id= $_GET['id'];
			//准备sql语句 执行sql语句
			$sql ="delete from news where id ={$id}";

			$result = mysql_query($sql);

			if(mysql_affected_rows()>0){

				echo "<script type='text/javascript'>alert('删除成功');window.location='./index.php'</script>";
			}else{

				echo "<script type='text/javscript'>alert('删除失败');window.location='./index.php'</script>";
			}
	
		break;

		case 'update': //执行修改操作
			$id = $_GET['id'];

			$title = $_POST['title'];
			$keywords = $_POST['keywords'];
			$author = $_POST['author'];
			$content = $_POST['content'];

			//准备sql语句
			$sql = "update news set  title='{$title}',keywords='{$keywords}',author='{$author}',content = '{$content}' where id = {$id}";

			//echo $sql;
			$result = mysql_query($sql);

			if(mysql_affected_rows()>0){

				echo "<script type=text/javascript>alert('修改成功');window.location='./index.php'</script>";

			}else{

				echo "<script type='text/javascript'>alert('修改失败');window.location='./edit.php'</script>";
			}
		break;

	}


	//4.关闭数据库
	mysql_close();

 ?>